package com.naja.cdc.develop.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.naja.cdc.framework.provider.datasource.DatasourceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @auther wangjianying
 * @date 2024/02/04 16:25
 */
@Component
public class DynamicDatasourceProvider implements DatasourceProvider {
    @Autowired
    private DataSource dataSource;
    @Override
    public DataSource getDataSource(String name) {
        DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
        try {
            return dynamicRoutingDataSource.getDataSource(name).unwrap(DruidDataSource.class);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
